package com.tmall.tmall.controller.admin;

import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/product")
public class AdminProductController {
    @Autowired
    private ProductService productService;

    /**
     * 新增商品
     * @param productGt
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody ProductGt productGt){
        productService.insert(productGt);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param productTypeId
     * @return
     */
    @GetMapping("/page")
    public Result Page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,Integer productTypeId,Integer qwe){
        PageRt list = productService.page(pageNum,pageSize,name,productTypeId,qwe);
        return Result.success(list);
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/delBatch")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        productService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查找商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        ProductRt productRt = productService.getById(id);
        return Result.success(productRt);
    }

    /**
     * 修改商品
     * @param productGt
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody ProductGt productGt){
        productService.update(productGt);
        return Result.success();
    }

    /**
     * 获取所有商品信息
     * @return
     */
    @GetMapping("list")
    public Result list(){
        List<ProductRt> productRts=productService.selectAll();
        return Result.success(productRts);
    }
}
